﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using weikebuluo.DAL;
using weikebuluo.Common;
using weikebuluo.Model;

namespace weikebuluo.web.wechat.vip
{
    public partial class baoming : System.Web.UI.Page
    {
        v_vip_dal vipdal = new v_vip_dal();
        protected v_vip_model vipmodel = new v_vip_model();
        protected DataTable dtvip = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!string.IsNullOrEmpty(Request.QueryString["wxid"]))
            {
                dtvip = vipdal.GetList("openid='" + Request.QueryString["wxid"] + "'").Tables[0];
                if (dtvip.Rows.Count > 0)
                {
                    vipmodel = vipdal.GetModel(int.Parse(dtvip.Rows[0]["vip_id"].ToString()));
                    if (!string.IsNullOrEmpty(Request.QueryString["hid"]))
                    {
                        Response.Write("<script Language=\"Javascript\">location.href=\"/web/wechat/guaguaka/guaguaka.aspx?wxid=" + Request.QueryString["wxid"] + "&uid=" + Request.QueryString["uid"] + "&hid=" + Request.QueryString["hid"] + "\";");
                        Response.Write("</script>");
                        Response.End();
                    }
                }

                switch (Request.QueryString["action"]) { 
                    case"bindvip":
                        submit();
                        break;
                }
            }
        }

        private void submit()
        { //领卡
            
            dtvip = vipdal.GetList("openid='" + Request.QueryString["wxid"] + "'").Tables[0];
            if (Request.QueryString["wxid"].Trim().ToString() == "null")
            {
                Response.Write("{\"error\":\"请从微信登录！\"}");
                Response.End();
            }

            if (dtvip.Rows.Count > 0)
            {
                vipmodel = vipdal.GetModel(int.Parse(dtvip.Rows[0]["vip_id"].ToString()));
                vipmodel.vip_name = Request.Form["vip_name"].Trim().ToString();
                vipmodel.vip_phone = Request.Form["vip_phone"].Trim().ToString();
                vipmodel.vip_integral = int.Parse(Request.Form["vip_integral"].Trim().ToString());
                vipmodel.vip_qq = Request.Form["vip_qq"].Trim().ToString();
                vipmodel.vip_email = Request.Form["vip_email"].Trim().ToString();
                vipmodel.vip_company = Request.Form["vip_company"].Trim().ToString();
                vipmodel.vip_position = Request.Form["vip_position"].Trim().ToString();
                vipmodel.vip_address = Request.Form["vip_address"].Trim().ToString();
                vipmodel.vip_area = Request.Form["vip_area"].Trim().ToString();
                vipmodel.user_id = int.Parse(Request.QueryString["uid"].ToString());
                vipdal.Update(vipmodel);

                Response.Write("{\"error\":\"200\",\"msg\":\"\",\"url\":\"baoming.aspx?wxid=" + Request.QueryString["wxid"] + "&uid=" + Request.QueryString["uid"] + "\"}");
                Response.End();

            }else{
                DataTable dt = vipdal.GetList("vip_phone='" + Request.Form["phone"] + "'").Tables[0];
                if (dt.Rows.Count > 0)
                {
                    Response.Write("{\"error\":\"电话号码已经注册！\"}");
                    Response.End();
                }

                vipmodel.vip_name = Request.Form["vip_name"].Trim().ToString();
                vipmodel.vip_phone = Request.Form["vip_phone"].Trim().ToString();
                vipmodel.vip_integral = int.Parse(Request.Form["vip_integral"].Trim().ToString());
                vipmodel.vip_qq = Request.Form["vip_qq"].Trim().ToString();
                vipmodel.vip_email = Request.Form["vip_email"].Trim().ToString();
                vipmodel.vip_company = Request.Form["vip_company"].Trim().ToString();
                vipmodel.vip_position = Request.Form["vip_position"].Trim().ToString();
                vipmodel.vip_address = Request.Form["vip_address"].Trim().ToString();
                vipmodel.vip_area = Request.Form["vip_area"].Trim().ToString();
                vipmodel.user_id = int.Parse(Request.QueryString["uid"].ToString());
                vipmodel.openid = Request.QueryString["wxid"].Trim().ToString();
                if (vipdal.Add(vipmodel) > 0)
                {
                    if (!string.IsNullOrEmpty(Request.QueryString["hid"]))
                    {
                        Response.Write("{\"error\":\"100\",\"msg\":\"\",\"url\":\"/web/wechat/guaguaka/guaguaka.aspx?wxid=" + Request.QueryString["wxid"] + "&uid=" + Request.QueryString["uid"] + "&hid=" + Request.QueryString["hid"] + "\"}");
                        Response.End();
                    }
                    else
                    {
                        Response.Write("{\"error\":\"200\",\"msg\":\"\",\"url\":\"baoming.aspx?wxid=" + Request.QueryString["wxid"] + "&uid=" + Request.QueryString["uid"] + "\"}");
                        Response.End();
                    }
                }
                else
                {
                    Response.Write("{\"error\":\"提交失败！\"}");
                    Response.End();
                }
            }
        }
    }
}